package com.stox.shared.core.util.converter;

import com.stox.shared.core.model.Bar;

public class BarConverter implements Converter<Bar> {
	
	public static final BarConverter INSTANCE = new BarConverter();

	private final int id = 1;
	
	private BarConverter() {
		
	}
	
	@Override
	public String encode(Bar bar) {
		return id+","+bar.getScripId()+","+bar.toString();
	}

	@Override
	public Bar decode(String message) {
		String[] tokens = message.split(",");
		Long scripId = Long.parseLong(tokens[1]);
		return new Bar(scripId, message.substring((id+","+scripId).length()+1));
	}
	
	@Override
	public boolean supports(String message) {
		return message.startsWith(String.valueOf(id));
	}
	
	public int getId() {
		return id;
	}

}
